<?php

/**
 * This is the model class for table "{{material_requisition_item}}".
 *
 * The followings are the available columns in table '{{material_requisition_item}}':
 * @property integer $id
 * @property integer $materialRequisitionId
 * @property integer $materialModelId
 * @property string $brand
 * @property string $amount
 * @property string $unit
 * @property integer $applicantId
 * @property integer $amountStandardUnit
 * @property string $note
 * @property integer $lateSubmit
 * @property string $createTime
 * @property integer $createUserId
 * @property string $updateTime
 * @property integer $updateUserId
 *
 * The followings are the available model relations:
 * @property MaterialRequisition $materialRequisition
 * @property Employee $applicant
 * @property MaterialModel $materialModel
 */
class MaterialRequisitionItem extends CActiveRecord {
	/**
	 * Returns the static model of the specified AR class.
	 * @return MaterialRequisitionItem the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return '{{material_requisition_item}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array(
				'materialModelId, applicantId, unit, amount',
				'required'
			),
			array(
				'materialModelId, applicantId, lateSubmit',
				'numerical',
				'integerOnly' => true
			),
			array(
				'materialModelId+materialRequisitionId+applicantId+unit',
				'components.db.UniqueMultiColumnValidator',
				'errorAttributes' => array(
					'materialModelId'
				),
			),
			array(
				'brand, unit, note',
				'length',
				'max' => 255
			),
			array(
				'amount, amountStandardUnit',
				'numerical',
			),
			array(
				'amount, amountStandardUnit',
				'length',
				'max' => 16
			),
			array(
				'amountStandardUnit',
				'checkStandardUnit',
			),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array(
				'id, materialRequisitionId, materialModelId, brand, amount, unit, applicantId, amountStandardUnit, note, lateSubmit, createTime, createUserId, updateTime, updateUserId',
				'safe',
				'on' => 'search'
			),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'materialRequisition' => array(
				self::BELONGS_TO,
				'MaterialRequisition',
				'materialRequisitionId'
			),
			'applicant' => array(
				self::BELONGS_TO,
				'Employee',
				'applicantId'
			),
			'materialModel' => array(
				self::BELONGS_TO,
				'MaterialModel',
				'materialModelId',
				'with' => 'material'
			),
		);
	}

	public function behaviors() {
		return array(
			'AuditableBehavior' => array(
				'class' => 'components.db.AuditableBehavior',
				'attributes' => 'materialRequisitionId, materialModelId, brand, amount, unit, applicantId, amountStandardUnit, note, lateSubmit'
			),
			'AuditStampBehavior' => array(
				'class' => 'components.db.AuditStampBehavior',
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'materialRequisitionId' => '申购单',
			'materialModelId' => '物品型号',
			'brand' => '品牌/产地',
			'amount' => '数量',
			'unit' => '单位',
			'applicantId' => '申购人',
			'amountStandardUnit' => '合标准数量',
			'note' => '备注',
			'lateSubmit' => '是否补交',
			'createTime' => '创建时间',
			'createUserId' => '创建用户',
			'updateTime' => '更新时间',
			'updateUserId' => '更新用户',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('id', $this->id);
		$criteria->compare('materialRequisitionId', $this->materialRequisitionId);
		$criteria->compare('materialModelId', $this->materialModelId);
		$criteria->compare('brand', $this->brand, true);
		$criteria->compare('amount', $this->amount, true);
		$criteria->compare('unit', $this->unit, true);
		$criteria->compare('applicantId', $this->applicantId);
		$criteria->compare('amountStandardUnit', $this->amountStandardUnit);
		$criteria->compare('note', $this->note, true);
		$criteria->compare('lateSubmit', $this->lateSubmit);
		$criteria->compare('createTime', $this->createTime, true);
		$criteria->compare('createUserId', $this->createUserId);
		$criteria->compare('updateTime', $this->updateTime, true);
		$criteria->compare('updateUserId', $this->updateUserId);

		return new CActiveDataProvider($this, array(
			'criteria' => $criteria,
		));
	}

	public function checkStandardUnit($attribute, $params) {
		if ($this->unit && $this->materialModelId) {
			if ($this->unit == $this->materialModel->unit) {
				if ($this->amountStandardUnit) {
					if ($this->amountStandardUnit != $this->amount) {
						$this->addError('amountStandardUnit', '物品单位和标准单位相同，但是数量不一致');
					}
				} else {
					$this->amountStandardUnit = $this->amount;
				}
			} else {
				if (!$this->amountStandardUnit) {
					$this->addError('amountStandardUnit', '请输入折合数量');
				}
			}
		}
	}
}
